package com.netcracker.ui.command;

import com.netcracker.entity.Book;
import com.netcracker.util.service.BookService;
import com.netcracker.util.service.ServiceFacade;
import com.netcracker.util.service.exception.DataTransmittingException;
import com.netcracker.util.service.exception.NullDataException;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.slf4j.LoggerFactory;

public class FindBookCommand implements Command {
    private static final org.slf4j.Logger LOGGER = LoggerFactory.getLogger(FindBookCommand.class);
    private final ServiceFacade serviceFacade;

        public FindBookCommand(ServiceFacade serviceFacade) {
        this.serviceFacade = serviceFacade;
    }
    
    @Override
    public String execute(HttpServletRequest request, HttpServletResponse response) {
        String url = "index";
        if (request.getParameter("type") != null) {
            String type = request.getParameter("type");
            List<Book> books = null;
            BookService bookService = serviceFacade.getBookService();
            switch(type) {
                case "byAuthor":
                    String author = request.getParameter("author"); 
                    try { 
                        books = bookService.findBooksByAuthor(author);
                    } catch (NullDataException ex) {
                        request.setAttribute("error", "Книг не найдено.");
                    } catch (DataTransmittingException ex) {
                        request.setAttribute("error", "Ошибка обращения к БД.");
                        LOGGER.error("DB error: ", ex);
                    }
                    url = "ResultsFound.jsp";
                    break;
                case "byName":
                    String name = request.getParameter("name"); 
                    try { 
                        books = bookService.findBooksByName(name);
                    } catch (NullDataException ex) {
                        request.setAttribute("error", "Книг не найдено.");
                    } catch (DataTransmittingException ex) {
                        request.setAttribute("error", "Ошибка обращения к БД.");
                        LOGGER.error("DB error: ", ex);
                    }
                    url = "ResultsFound.jsp";
                    break;
                case "ByUser":
                    Integer userID = (Integer) request.getSession().getAttribute("id");
                    try {
                        books = bookService.findBooksByUser(userID);
                    } catch (NullDataException ex) {
                        request.setAttribute("error", "Книг не найдено.");
                    } catch (DataTransmittingException ex) {
                        request.setAttribute("error", "Ошибка обращения к БД.");
                        LOGGER.error("DB error:", ex);
                    }
                    url = "ResultsFound.jsp";
                    break; 
                case "byGenre":
                    Integer genreId = Integer.parseInt(request.getParameter("genreId")); 
                    try { 
                        books = bookService.findBooksByGenre(genreId); 
                    } catch (NullDataException ex) {
                        request.setAttribute("error", "Книг не найдено.");
                    } catch (DataTransmittingException ex) {
                        request.setAttribute("error", "Ошибка обращения к БД.");
                        LOGGER.error("DB error: ", ex);
                    }
                    url = "ResultsFound.jsp";
                    break;
            }
            request.setAttribute("books", books);  
        } 
        return url;
    }   
}
